package com.hystrix.command;

import com.hystrix.model.InfoB;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;

/**
 * 嵌套fallback执行另一个command
 * 
 * 除了HystrixBadRequestException异常之外，所有从run()方法抛出的异常都算作失败，并触发降级getFallback()和断路器逻辑
 * 
 * 
 * @author wangxh
 *
 */
public class FallBackNestCommand extends HystrixCommand<String> {

	public FallBackNestCommand() {
		super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("FallBackGroupCommandKey"))
				.andCommandPropertiesDefaults(
						HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(500)));
	}

	@Override
	protected String run() throws Exception {
		Thread.sleep(1000l);
		return "FallBackExecuteCommand";
	}

	@Override
	protected String getFallback() {
		ServiceBInfoCommand bcommand = new ServiceBInfoCommand();
		InfoB infoB = bcommand.execute();
		return infoB.toString();
	}

}
